const timeutil = require('./timeutil.js');

function GetOpenData() {
  return new Promise(function (resolve, reject) {
    wx.cloud.callFunction({
      name: 'openapi',
      data: {
        action: 'getOpenData'
      },
      success: res => {
        //设置缓存
        wx.setStorageSync('userinfo', res.result);
        console.log('--缓存设置成功--');
        resolve(res.result.openid);
      },
      fail: res => {
        console.log('--缓存设置失败--')
        reject({
          msg: 'OPENDATA_GET_FAILURE'
        })
      }
    })
  })

}

function UploadImage(path) {
  return new Promise(function (resolve, reject) {
    wx.getFileInfo({
      filePath: path,
      success(ans) {
        wx.cloud.uploadFile({
          cloudPath: 'M_CIRCLE' + '/' + ans.digest + '.png',
          filePath: path,
          success: res => {
            resolve(res)
          },
          fail(res) {
            reject({
              msg: 'FAILED_TO_UPLOAD_IMG'
            })
          }
        })
      }
    })
  })
}

function UpCircleInfo(_res) {
  let db = wx.cloud.database();
  let app = getApp();
  console.log('--调用上传--')
  return new Promise(function (resolve, reject) {
    db.collection('M_CIRCLE_LIST').add({
      data: {
        create_time: timeutil.TimeCode(new Date()),
        update_time: timeutil.TimeCode(new Date()),
        content: _res.content,
        images: _res.realList,
        userid: app.globalData.openid,
        thumbsnum: 0,
        remarksnum: 0,
        nickname: _res.userinfo.nickName,
        avatar: _res.userinfo.avatarUrl,
      },
      success: res => {
        console.log(res);
        resolve(res)
      },
      fail: res => {
        console.log(res);
        reject({
          msg: 'UPLOAD_CIRCLE_INFO_FAILED'
        });
      }
    })
  })
}

function QueryCircleInfo(skipstep) {
  let db = wx.cloud.database();
  return new Promise(function (resolve, reject) {
    db.collection('M_CIRCLE_LIST').skip(skipstep).orderBy('update_time', 'desc').get({
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject({
          msg: 'FAILED_TO_GET_CIRCLE_LIST'
        })
      }
    })
  })
}

function GetOneCircleInfo(cid) {
  let db = wx.cloud.database();
  return new Promise(function (resolve, reject) {
    db.collection('M_CIRCLE_LIST').doc(cid).get({
      success: res => {
        resolve(res)
      },
      fail: res => {
        reject({
          msg: 'FAILED_TO_GET_THIS_CIRCLE_LIST'
        })
      }
    })
  })
}

function UpCommentInfo(replyid, commentText, Safe) {
  let app = getApp();
  return new Promise(function (resolve, reject) {
    wx.cloud.callFunction({
      name: 'mapi',
      data: {
        action: 'AddCommentInfo',
        data: {
          create_time: timeutil.TimeCode(new Date()),
          update_time: timeutil.TimeCode(new Date()),
          circle_id: replyid,
          nickname: app.globalData.userInfo.nickName,
          avatar: app.globalData.userInfo.avatarUrl,
          content: commentText,
          userid: app.globalData.openid
        }
      },
      success:res=>{
        resolve(res)
      },
      fail:res=>{
        reject({msg:'FAILED_TO_COMMENT'});
      }
    })
  })
  //'AddCommentInfo'
}
function GetCommentInfo(cid){
  let db = wx.cloud.database();
  return new Promise(function (resolve, reject) {
    db.collection('M_REMARK_LIST').where({
      circle_id:cid
    }).get({
      success:res=>{
        resolve(res)
      },
      fail:res=>{
        reject({msg:'FAILED_TO_LOAD_COMMENT'});
      }
    })
  })
}
function UpThumbInfo(cid){
  let app = getApp();
  return new Promise(function (resolve, reject) {
    wx.cloud.callFunction({
      name: 'mapi',
      data: {
        action: 'AddThumbInfo',
        data: {
          create_time: timeutil.TimeCode(new Date()),
          update_time: timeutil.TimeCode(new Date()),
          circle_id: cid,
          nickname: app.globalData.userInfo.nickName,
          avatar: app.globalData.userInfo.avatarUrl,
          userid: app.globalData.openid
        }
      },
      success:res=>{
        resolve(res)
      },
      fail:res=>{
        reject({msg:'FAILED_TO_LIKE'});
      }
    })
  })
}
function GetThumbInfo(cid){
  let app = getApp();
  let db = wx.cloud.database();
  return new Promise(function(resolve,reject){
    db.collection('M_THUMBS_LIST').where({
      circle_id:cid,
      userid:app.globalData.openid
    }).get({
      success:res=>{
        resolve(res)
      },
      fail:res=>{
        reject({msg:'FAILED_TO_GET_LIKE'});
      }
    })
  })
}
function DelCircleInfo(id){
  let app = getApp();
  let db = wx.cloud.database();
  return new Promise(function(resolve,reject){
    wx.cloud.callFunction({
      name:'mapi',
      data:{
        action:'DelCircleInfo',
        id:id
      },
      success:res=>{
        resolve(res)
      },fail:res=>{
        console.log(res)
        reject({msg:'FAILED_DEL'})
      }
    })
  })
}
module.exports = {
  GetOpenData,
  UploadImage,
  UpCircleInfo,
  QueryCircleInfo,
  GetOneCircleInfo,
  UpCommentInfo,
  GetCommentInfo,
  GetThumbInfo,
  UpThumbInfo,
  DelCircleInfo
}